package com.erp.wallet.controller;

import com.erp.wallet.domain.bo.ErpSellerStoreWalletBo;
import com.erp.wallet.domain.vo.ErpSellerStoreWalletVo;
import com.erp.wallet.service.IErpSellerStoreWalletService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.ExpenseType;
import com.ruoyi.common.core.enums.FlowType;
import com.ruoyi.common.core.enums.PayType;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wallet.api.domain.ErpSellerStoreWallet;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * erp用户钱包
 *
 * @author erp
 * @date 2023-05-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/seller/sellerStoreWallet")
public class ErpSellerStoreWalletController extends BaseController {

	private final IErpSellerStoreWalletService iErpSellerStoreWalletService;

	/**
	 * 查询erp用户钱包列表
	 */
	@GetMapping("/list")
	public TableDataInfo<ErpSellerStoreWalletVo> list(ErpSellerStoreWalletBo bo, PageQuery pageQuery) {
		return iErpSellerStoreWalletService.queryPageList(bo, pageQuery);
	}

	/**
	 * 导出erp用户钱包列表
	 */
	@PostMapping("/export")
	public void export(ErpSellerStoreWalletBo bo, HttpServletResponse response) {
		List<ErpSellerStoreWalletVo> list = iErpSellerStoreWalletService.queryList(bo);
        ExcelUtil<ErpSellerStoreWalletVo> excelUtil = new ExcelUtil<>(ErpSellerStoreWalletVo.class);
        excelUtil.exportExcel(response,list,"erp用户钱包");
	}

	/**
	 * 获取erp用户钱包详细信息
	 *
	 * @param id 主键
	 */
	@GetMapping("/{id}")
	public R<ErpSellerStoreWalletVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
		return R.ok(iErpSellerStoreWalletService.queryById(id));
	}

	/**
	 * 新增erp用户钱包
	 */
	@RepeatSubmit()
	@PostMapping()
	public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreWalletBo bo) {
		return toAjax(iErpSellerStoreWalletService.insertByBo(bo));
	}

	/**
	 * 修改erp用户钱包
	 */
	@RepeatSubmit()
	@PutMapping()
	public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreWalletBo bo) {
		return toAjax(iErpSellerStoreWalletService.updateByBo(bo));
	}

	/**
	 * 删除erp用户钱包
	 *
	 * @param ids 主键串
	 */
	@DeleteMapping("/{ids}")
	public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
		return toAjax(iErpSellerStoreWalletService.deleteWithValidByIds(Arrays.asList(ids), true));
	}

	/**
	 * 根据卖家id获取钱包
	 * 
	 * @return
	 */
	@GetMapping("/findBySellerId")
	public R<Object> findBySellerId() {
		// 获取当前登录的用户id
		Long sellerId = SecurityUtils.getUserId();
//		Long sellerId = 1l;
		System.out.println("sellerId:" + sellerId);
		ErpSellerStoreWallet wallet = iErpSellerStoreWalletService.findBySellerId(sellerId);
		return R.ok(wallet);
	}

	/**
	 * chatgpt扣除余额
	 * @return
	 */
	@GetMapping("/chatGptCharging")
	public R<Object> chatGptCharging() {

		Long sellerId = SecurityUtils.getUserId();

		String orderNo = System.currentTimeMillis() + "";

		return iErpSellerStoreWalletService.updateWallet(FlowType.DEDUCTION.getCode(), ExpenseType.CHATGPT.getCode(),
				PayType.YE.getCode(), BigDecimal.valueOf(0.05), sellerId, orderNo, null);

	}
    
    @GetMapping("/updateWallet/{flowType}/{expenseType}/{payType}/{money}/{sellerId}/{orderNo}/{remark}")
    public R<Object> updateWallet(@PathVariable("flowType") Integer flowType,@PathVariable("expenseType") Integer expenseType,
                                  @PathVariable("payType") Integer payType, @PathVariable("money") BigDecimal money,
                                  @PathVariable("sellerId") Long sellerId,@PathVariable("orderNo") String orderNo,
                                  @PathVariable("remark") String remark) {
        return iErpSellerStoreWalletService.updateWallet(flowType, expenseType, payType, money, sellerId, orderNo, remark);
    }
    
    @GetMapping("/initWallet/{userId}")
    public ErpSellerStoreWallet initWallet(@PathVariable("userId") Long userId) {
        return iErpSellerStoreWalletService.init(userId);
    }

}
